package fr.awtl.framework;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Factory {

	private static Factory factory;
	
	public static Factory getInstance(){
		if(factory==null){
			factory = new Factory();
		}
		return factory;
	}
	
	private Map<String, ActionPackage> actionsMap;
	
	ActionPackage getActionPackage(String pattern) {
		return actionsMap.get(pattern);
	}

	void fillMap(String fichier) throws ParserConfigurationException, SAXException, IOException, DOMException, ClassNotFoundException {

		if(actionsMap==null){
			
			actionsMap = new HashMap<String, ActionPackage>();
			
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(fichier);
			
			Element racine = document.getDocumentElement();
			
			Map<String, String> formsMap = new HashMap<String, String>();
			
			NodeList forms = racine.getElementsByTagName("form");
			for (int i = 0; i < forms.getLength(); i++) {
				Node form = forms.item(i);
				String formName = "";
				String formClass = "";
				NodeList elements = form.getChildNodes();
				for(int j = 0; j < elements.getLength();j++){
					Node element = elements.item(j);
					if(element.getNodeName().equals("form-class")){
						formClass=element.getTextContent();
					}
					else if(element.getNodeName().equals("form-name")){
						formName=element.getTextContent();
					}
				}
				if(!formClass.equals("") && !formName.equals("")){
					formsMap.put(formName, formClass);
				}
			}
			
			
			NodeList actions = racine.getElementsByTagName("action");
			for (int i = 0; i < actions.getLength(); i++) {
				Node action = actions.item(i);
				NodeList elements = action.getChildNodes();
				String actionName = "";
				String urlPattern = "";
				String formName = "";
				String input = "";
				for(int j = 0; j < elements.getLength();j++){
					Node element = elements.item(j);
					if(element.getNodeName().equals("form-name")){
						formName = element.getTextContent();
					}
					else if (element.getNodeName().equals("action-name")){
						actionName = element.getTextContent();
					}
					else if(element.getNodeName().equals("url-pattern")){
						urlPattern = element.getTextContent();
					}
					else if(element.getNodeName().equals("input")){
						input=element.getTextContent();
					}
				}
				if(!formName.equals("") && !actionName.equals("") && !urlPattern.equals("") && !input.equals("")){
					if(formsMap.get(formName)!=null){
						ActionPackage pack = new ActionPackage();
						pack.setActionName(actionName);
						pack.setFormClass(formsMap.get(formName));
						pack.setInput(input);
						pack.setFormName(formName);
						actionsMap.put(urlPattern, pack);
					}
				}
			}
		}
	}
	
}
